﻿using HuaCheng.CommonClass;
using HuaCheng.Model;
using System;

namespace HuaCheng.Controller
{
    public static class ChannelStateControl
    {
        /// <summary>
        /// 从ARM接受到的数据解析相应通道的状态
        /// </summary>
        /// <param name="stateData">总的状态数据包</param>
        /// <param name="chNum">通道号</param>
        /// <param name="chStateLength">通道状态数据长度</param>
        /// <returns>通道状态类</returns>
        //public static ChannelState byte2Channel(byte[] stateData, int chNum, int chStateLength)
        //{
        //    ChannelState tmpChannel = new ChannelState();
        //    try
        //    {
        //        byte[] chStateArray = new byte[chStateLength];
        //        Array.Copy(stateData, 2 + chNum * chStateLength, chStateArray, 0, chStateLength);//拷贝通道数据
        //        tmpChannel.StepNum = BitConverter.ToUInt16(chStateArray, 0) >> 4;
        //        tmpChannel.MID = BitConverter.ToUInt16(chStateArray, 2) & 0x0FFF;
        //        //tmpChannel.CID = BitConverter.ToUInt16(chStateArray, 2) >> 12;
        //        tmpChannel.CID = chNum;
        //        //AC状态1
        //        tmpChannel.ACstate1_BusUnderVolt = (chStateArray[4] & 0x01) == 0x01 ? true : false;
        //        tmpChannel.ACstate1_PhaseError = (chStateArray[4] & 0x02) == 0x02 ? true : false;
        //        tmpChannel.ACstate1_ACUnderVolt = (chStateArray[4] & 0x04) == 0x04 ? true : false;
        //        tmpChannel.ACstate1_ACLoseVolt = (chStateArray[4] & 0x08) == 0x08 ? true : false;
        //        tmpChannel.ACstate1_rsv4 = (chStateArray[4] & 0x10) == 0x10 ? true : false;
        //        tmpChannel.ACstate1_rsv5 = (chStateArray[4] & 0x20) == 0x20 ? true : false;
        //        tmpChannel.ACstate1_rsv6 = (chStateArray[4] & 0x40) == 0x40 ? true : false;
        //        tmpChannel.ACstate1_rsv7 = (chStateArray[4] & 0x80) == 0x80 ? true : false;
        //        tmpChannel.ACstate1_rsv8 = (chStateArray[5] & 0x01) == 0x01 ? true : false;
        //        tmpChannel.ACstate1_AC_fuse = (chStateArray[5] & 0x02) == 0x02 ? true : false;
        //        tmpChannel.ACstate1_emergency = (chStateArray[5] & 0x04) == 0x04 ? true : false;
        //        tmpChannel.ACstate1_AC_overTemper = (chStateArray[5] & 0x08) == 0x08 ? true : false;
        //        tmpChannel.ACstate1_AC_ACOverVoltHard = (chStateArray[5] & 0x10) == 0x10 ? true : false;
        //        tmpChannel.ACstate1_AC_DCOverVoltHard = (chStateArray[5] & 0x20) == 0x20 ? true : false;
        //        tmpChannel.ACstate1_AC_ACOverCurrentHard = (chStateArray[5] & 0x40) == 0x40 ? true : false;
        //        tmpChannel.ACstate1_AC_paramError = (chStateArray[5] & 0x80) == 0x80 ? true : false;

        //        //AC状态2
        //        tmpChannel.ACstate2_AC_ACOverVoltSoft = (chStateArray[6] & 0x01) == 0x01 ? true : false;
        //        tmpChannel.ACstate2_AC_DCOverVoltSoft = (chStateArray[6] & 0x02) == 0x02 ? true : false;
        //        tmpChannel.ACstate2_AC_ACOverCurrentSoft = (chStateArray[6] & 0x04) == 0x04 ? true : false;
        //        tmpChannel.ACstate2_AC_breakFault = (chStateArray[6] & 0x08) == 0x08 ? true : false;
        //        tmpChannel.ACstate2_PLL = (chStateArray[6] & 0x10) == 0x10 ? true : false;
        //        tmpChannel.ACstate2_AC_highVolt = (chStateArray[6] & 0x20) == 0x20 ? true : false;
        //        tmpChannel.ACstate2_gridFault = (chStateArray[6] & 0x40) == 0x40 ? true : false;
        //        tmpChannel.ACstate2_AC_preCharge = (chStateArray[6] & 0x80) == 0x80 ? true : false;
        //        tmpChannel.ACstate2_AC_breakState = (chStateArray[7] & 0x01) == 0x01 ? true : false;
        //        tmpChannel.ACstate2_rsv0 = (chStateArray[7] & 0x02) == 0x02 ? true : false;
        //        tmpChannel.ACstate2_AC_workState = (chStateArray[7] & 0x04) == 0x04 ? true : false;
        //        tmpChannel.ACstate2_AC_fault = (chStateArray[7] & 0x08) == 0x08 ? true : false;
        //        tmpChannel.ACstate2_AC_preChargeFault = (chStateArray[7] & 0x10) == 0x10 ? true : false;
        //        tmpChannel.ACstate2_AC_UPS = (chStateArray[7] & 0x20) == 0x20 ? true : false;
        //        tmpChannel.ACstate2_DC_dischargeState = (chStateArray[7] & 0x40) == 0x40 ? true : false;
        //        tmpChannel.ACstate2_DC_dischargeError = (chStateArray[7] & 0x80) == 0x80 ? true : false;

        //        tmpChannel.channelEfficiency = BitConverter.ToUInt16(chStateArray, 8) * 1000;

        //        tmpChannel.DCstate1_DC_workState = (chStateArray[10] & 0x01) == 0x01 ? true : false;
        //        tmpChannel.DCstate1_rsv0 = (chStateArray[10] & 0x02) == 0x02 ? true : false;
        //        tmpChannel.DCstate1_DC_fault = (chStateArray[10] & 0x04) == 0x04 ? true : false;
        //        tmpChannel.DCstate1_rsv1 = (chStateArray[10] & 0x08) == 0x08 ? true : false;
        //        tmpChannel.DCstate1_rsv2 = (chStateArray[10] & 0x10) == 0x10 ? true : false;
        //        tmpChannel.DCstate1_DC_preChargeFault = (chStateArray[10] & 0x20) == 0x20 ? true : false;
        //        tmpChannel.DCstate1_rsv3 = (chStateArray[10] & 0x40) == 0x40 ? true : false;
        //        tmpChannel.DCstate1_rsv4 = (chStateArray[10] & 0x80) == 0x80 ? true : false;
        //        tmpChannel.DCstate1_Contactor = (chStateArray[11] & 0x01) == 0x01 ? true : false;
        //        tmpChannel.DCstate1_rsv5 = (chStateArray[11] & 0x02) == 0x02 ? true : false;
        //        tmpChannel.DCstate1_Fan = (chStateArray[11] & 0x04) == 0x04 ? true : false;
        //        tmpChannel.DCstate1_rsv6 = (chStateArray[11] & 0x08) == 0x08 ? true : false;
        //        tmpChannel.DCstate1_rsv7 = (chStateArray[11] & 0x10) == 0x10 ? true : false;
        //        tmpChannel.DCstate1_rsv8 = (chStateArray[11] & 0x20) == 0x20 ? true : false;
        //        tmpChannel.DCstate1_rsv9 = (chStateArray[11] & 0x40) == 0x40 ? true : false;
        //        tmpChannel.DCstate1_rsv10 = (chStateArray[11] & 0x80) == 0x80 ? true : false;

        //        //DC状态
        //        tmpChannel.DCstate_rsv0 = (chStateArray[12] & 0x01) == 0x01 ? true : false;
        //        tmpChannel.DCstate_rsv1 = (chStateArray[12] & 0x02) == 0x02 ? true : false;
        //        tmpChannel.DCstate_rsv2 = (chStateArray[12] & 0x04) == 0x04 ? true : false;
        //        tmpChannel.DCstate_rsv3 = (chStateArray[12] & 0x08) == 0x08 ? true : false;
        //        tmpChannel.DCstate_DC_breakFault = (chStateArray[12] & 0x10) == 0x10 ? true : false;
        //        tmpChannel.DCstate_rsv4 = (chStateArray[12] & 0x20) == 0x20 ? true : false;
        //        tmpChannel.DCstate_DC_inputOverVoltHard = (chStateArray[12] & 0x40) == 0x40 ? true : false;
        //        tmpChannel.DCstate_DC_outputOverVoltHard = (chStateArray[12] & 0x80) == 0x80 ? true : false;
        //        tmpChannel.DCstate_DC_loadOverCurrentHard = (chStateArray[13] & 0x01) == 0x01 ? true : false;
        //        tmpChannel.DCstate_DC_inductorOverCurrentHard = (chStateArray[13] & 0x02) == 0x02 ? true : false;
        //        tmpChannel.DCstate_DC_inputOverVoltSoft = (chStateArray[13] & 0x04) == 0x04 ? true : false;
        //        tmpChannel.DCstate_DC_outputOverVoltSoft = (chStateArray[13] & 0x08) == 0x08 ? true : false;
        //        tmpChannel.DCstate_DC_loadOverCurrentSoft = (chStateArray[13] & 0x10) == 0x10 ? true : false;
        //        tmpChannel.DCstate_DC_inductorOverCurrentSoft = (chStateArray[13] & 0x20) == 0x20 ? true : false;
        //        tmpChannel.DCstate_DC_overTemper = (chStateArray[13] & 0x40) == 0x40 ? true : false;
        //        tmpChannel.DCstate_DC_highVolt = (chStateArray[13] & 0x80) == 0x80 ? true : false;

        //        //DC停机原因
        //        tmpChannel.DCoff_voltageUpper = (chStateArray[14] & 0x01) == 0x01 ? true : false;
        //        tmpChannel.DCoff_DCoffvoltageLower = (chStateArray[14] & 0x02) == 0x02 ? true : false;
        //        tmpChannel.DCoff_currentUpper = (chStateArray[14] & 0x04) == 0x04 ? true : false;
        //        tmpChannel.DCoff_currentLower = (chStateArray[14] & 0x08) == 0x08 ? true : false;
        //        tmpChannel.DCoff_timeout = (chStateArray[14] & 0x10) == 0x10 ? true : false;
        //        tmpChannel.DCoff_temperUpper = (chStateArray[14] & 0x20) == 0x20 ? true : false;
        //        tmpChannel.DCoff_temperLower = (chStateArray[14] & 0x40) == 0x40 ? true : false;
        //        tmpChannel.DCoff_systemFault = (chStateArray[14] & 0x80) == 0x80 ? true : false;
        //        tmpChannel.DCoff_offCommand = (chStateArray[15] & 0x01) == 0x01 ? true : false;
        //        tmpChannel.DCoff_emergency = (chStateArray[15] & 0x02) == 0x02 ? true : false;
        //        tmpChannel.DCoff_paramError = (chStateArray[15] & 0x04) == 0x04 ? true : false;
        //        tmpChannel.DCoff_BusUnderVolt = (chStateArray[15] & 0x08) == 0x08 ? true : false;
        //        tmpChannel.DCoff_LastPackEnd = (chStateArray[15] & 0x10) == 0x10 ? true : false;
        //        tmpChannel.DCoff_ARM_auxTemper = (chStateArray[15] & 0x20) == 0x20 ? true : false;
        //        tmpChannel.DCoff_ARM_auxVolt = (chStateArray[15] & 0x40) == 0x40 ? true : false;
        //        tmpChannel.DCoff_ARM_emergency = (chStateArray[15] & 0x80) == 0x80 ? true : false;

        //        tmpChannel.DC_outputVolt = BitConverter.ToSingle(chStateArray, 16);
        //        tmpChannel.DC_loadCurrent = BitConverter.ToSingle(chStateArray, 20);
        //        if (BitConverter.ToUInt16(chStateArray, 24) >= 500 && BitConverter.ToUInt16(chStateArray, 24) < 6000)
        //        {
        //            tmpChannel.DC_loadTemper = Convert.ToSingle(1 / (Math.Log(2898.0f / (BitConverter.ToUInt16(chStateArray, 24)) - 0.4f) / 3470.0f + 1 / 298.15f) - 273.15f);
        //        }
        //        else tmpChannel.DC_loadTemper = 0;

        //        if (BitConverter.ToUInt16(chStateArray, 26) >= 500 && BitConverter.ToUInt16(chStateArray, 26) < 6000)
        //        {
        //            tmpChannel.DC_loadTemper2 = Convert.ToSingle(1 / (Math.Log(2898.0f / (BitConverter.ToUInt16(chStateArray, 26)) - 0.4f) / 3470.0f + 1 / 298.15f) - 273.15f);
        //        }
        //        else tmpChannel.DC_loadTemper2 = 0;

        //        tmpChannel.DC_busVolt = Convert.ToSingle(BitConverter.ToUInt16(chStateArray, 28)) / 1000.0f;
        //        tmpChannel.chargeCap = BitConverter.ToSingle(chStateArray, 30);
        //        tmpChannel.dischargeCap = BitConverter.ToSingle(chStateArray, 34);
        //        tmpChannel.totalCap = BitConverter.ToSingle(chStateArray, 38);
        //        tmpChannel.stepTime = BitConverter.ToSingle(chStateArray, 42);
        //        tmpChannel.chargeEnergy = BitConverter.ToSingle(chStateArray, 46);
        //        tmpChannel.dischargeEnergy = BitConverter.ToSingle(chStateArray, 50);
        //        tmpChannel.totalEnergy = BitConverter.ToSingle(chStateArray, 54);
        //        tmpChannel.power = BitConverter.ToSingle(chStateArray, 58);
        //        tmpChannel.diffVolt = BitConverter.ToSingle(chStateArray, 62);
        //        tmpChannel.diffCurrent = BitConverter.ToSingle(chStateArray, 66);
        //        tmpChannel.auxTemper = Convert.ToSingle(BitConverter.ToUInt16(chStateArray, 70));
        //        tmpChannel.auxVolt = BitConverter.ToSingle(chStateArray, 72);
        //        tmpChannel.faultStatus = fualtStatus2string(chStateArray);
        //    }
        //    catch(Exception e)
        //    {
        //        Console.WriteLine(e.StackTrace + e.Message);
        //    }
        //    return tmpChannel;
        //}
        /// <summary>
        /// 从ARM接受到的数据解析相应通道的状态
        /// </summary>
        /// <param name="stateData">总的状态数据包</param>
        /// <param name="chNum">通道号</param>
        /// <param name="chStateLength">通道状态数据长度</param>
        /// <returns>通道状态类</returns>
        public static ChannelState byte2Channel(byte[] stateData, int chNum, int chStateLength)
        {
            ChannelState tmpChannel = new ChannelState();
            try
            {
                tmpChannel.GID = BitConverter.ToInt16(stateData, 6);
                tmpChannel.MID = BitConverter.ToInt16(stateData, 8) & 0x00FF;
                tmpChannel.CID = chNum;
                byte[] chStateArray = new byte[chStateLength];
                Array.Copy(stateData, 14 + chNum * chStateLength, chStateArray, 0, chStateLength);//拷贝通道数据
                tmpChannel.StepScheme = BitConverter.ToInt16(chStateArray, 0);
                tmpChannel.StepNum = BitConverter.ToUInt16(chStateArray, 2);
                tmpChannel.channelEfficiency = BitConverter.ToUInt16(chStateArray, 4);

                //AC状态1
                tmpChannel.ACstate1_BusUnderVolt = (chStateArray[6] & 0x01) == 0x01 ? true : false;
                tmpChannel.ACstate1_PhaseError = (chStateArray[6] & 0x02) == 0x02 ? true : false;
                tmpChannel.ACstate1_ACUnderVolt = (chStateArray[6] & 0x04) == 0x04 ? true : false;
                tmpChannel.ACstate1_ACLoseVolt = (chStateArray[6] & 0x08) == 0x08 ? true : false;
                tmpChannel.ACstate1_AC_fuse = (chStateArray[6] & 0x10) == 0x10 ? true : false;
                tmpChannel.ACstate1_emergency = (chStateArray[6] & 0x20) == 0x20 ? true : false;
                tmpChannel.ACstate1_AC_overTemper = (chStateArray[6] & 0x40) == 0x40 ? true : false;
                tmpChannel.ACstate1_AC_ACOverVoltHard = (chStateArray[6] & 0x80) == 0x80 ? true : false;
                tmpChannel.ACstate1_AC_DCOverVoltHard = (chStateArray[7] & 0x01) == 0x01 ? true : false;
                tmpChannel.ACstate1_AC_ACOverCurrentHard = (chStateArray[7] & 0x02) == 0x02 ? true : false;
                tmpChannel.ACstate1_AC_paramError = (chStateArray[7] & 0x04) == 0x04 ? true : false;
                tmpChannel.ACstate1_AC_ACOverVoltSoft = (chStateArray[7] & 0x08) == 0x08 ? true : false;
                tmpChannel.ACstate1_AC_DCOverVoltSoft = (chStateArray[7] & 0x10) == 0x10 ? true : false;
                tmpChannel.ACstate1_AC_ACOverCurrentSoft = (chStateArray[7] & 0x20) == 0x20 ? true : false;
                tmpChannel.ACstate1_AC_breakFault = (chStateArray[7] & 0x40) == 0x40 ? true : false;
                tmpChannel.ACstate1_gridFault = (chStateArray[7] & 0x80) == 0x80 ? true : false;

                //AC状态2
                tmpChannel.ACstate2_AC_fault = (chStateArray[8] & 0x01) == 0x01 ? true : false;
                tmpChannel.ACstate2_AC_preChargeFault = (chStateArray[8] & 0x02) == 0x02 ? true : false;
                tmpChannel.ACstate2_AC_UPS = (chStateArray[8] & 0x04) == 0x04 ? true : false;
                tmpChannel.ACstate2_DC_dischargeError = (chStateArray[8] & 0x08) == 0x08 ? true : false;
                tmpChannel.ACstate2_PLL = (chStateArray[8] & 0x10) == 0x10 ? true : false;
                tmpChannel.ACstate2_AC_highVolt = (chStateArray[8] & 0x20) == 0x20 ? true : false;
                tmpChannel.ACstate2_AC_preCharge = (chStateArray[8] & 0x40) == 0x40 ? true : false;
                tmpChannel.ACstate2_AC_breakState = (chStateArray[8] & 0x80) == 0x80 ? true : false;
                tmpChannel.ACstate2_rsv8 = (chStateArray[9] & 0x01) == 0x01 ? true : false;
                tmpChannel.ACstate2_AC_workState = (chStateArray[9] & 0x02) == 0x02 ? true : false;
                tmpChannel.ACstate2_DC_dischargeState = (chStateArray[9] & 0x04) == 0x04 ? true : false;
                tmpChannel.ACstate2_rsv11 = (chStateArray[9] & 0x08) == 0x08 ? true : false;
                tmpChannel.ACstate2_rsv12 = (chStateArray[9] & 0x10) == 0x10 ? true : false;
                tmpChannel.ACstate2_rsv13 = (chStateArray[9] & 0x20) == 0x20 ? true : false;
                tmpChannel.ACstate2_rsv14 = (chStateArray[9] & 0x40) == 0x40 ? true : false;
                tmpChannel.ACstate2_rsv15 = (chStateArray[9] & 0x80) == 0x80 ? true : false;

                //DC状态1
                tmpChannel.DCstate1_DC_fault = (chStateArray[10] & 0x01) == 0x01 ? true : false;
                tmpChannel.DCstate1_DC_preChargeFault = (chStateArray[10] & 0x02) == 0x02 ? true : false;
                tmpChannel.DCstate1_dischargeError = (chStateArray[10] & 0x04) == 0x04 ? true : false;
                tmpChannel.DCstate1_DC_breakFault = (chStateArray[10] & 0x08) == 0x08 ? true : false;
                tmpChannel.DCstate1_DC_inputOverVoltHard = (chStateArray[10] & 0x10) == 0x10 ? true : false;
                tmpChannel.DCstate1_DC_outputOverVoltHard = (chStateArray[10] & 0x20) == 0x20 ? true : false;
                tmpChannel.DCstate1_DC_loadOverCurrentHard = (chStateArray[10] & 0x40) == 0x40 ? true : false;
                tmpChannel.DCstate1_DC_inductorOverCurrentHard = (chStateArray[10] & 0x80) == 0x80 ? true : false;
                tmpChannel.DCstate1_DC_inputOverVoltSoft = (chStateArray[11] & 0x01) == 0x01 ? true : false;
                tmpChannel.DCstate1_DC_outputOverVoltSoft = (chStateArray[11] & 0x02) == 0x02 ? true : false;
                tmpChannel.DCstate1_DC_loadOverCurrentSoft = (chStateArray[11] & 0x04) == 0x04 ? true : false;
                tmpChannel.DCstate1_DC_inductorOverCurrentSoft = (chStateArray[11] & 0x08) == 0x08 ? true : false;
                tmpChannel.DCstate1_DC_overTemper = (chStateArray[11] & 0x10) == 0x10 ? true : false;
                tmpChannel.DCstate1_emergency = (chStateArray[11] & 0x20) == 0x20 ? true : false;
                tmpChannel.DCstate1_paramError = (chStateArray[11] & 0x40) == 0x40 ? true : false;
                tmpChannel.DCstate1_BusUnderVolt = (chStateArray[11] & 0x80) == 0x80 ? true : false;

                //DC状态2
                tmpChannel.DCstate2_DC_highVolt = (chStateArray[12] & 0x01) == 0x01 ? true : false;
                tmpChannel.DCstate2_open_Volt = (chStateArray[12] & 0x02) == 0x02 ? true : false;
                tmpChannel.DCstate2_Chl_Enable = (chStateArray[12] & 0x04) == 0x04 ? true : false;
                tmpChannel.DCstate2_DC_workState = (chStateArray[12] & 0x08) == 0x08 ? true : false;
                tmpChannel.DCstate2_Contactor = (chStateArray[12] & 0x10) == 0x10 ? true : false;
                //tmpChannel.DCstate2_Contactor = (chStateArray[11] & 0x01) == 0x01 ? true : false;
                tmpChannel.DCstate2_DC_dischargeState = (chStateArray[12] & 0x20) == 0x20 ? true : false;
                tmpChannel.DCstate2_Temp_Data = BitConverter.ToUInt16(chStateArray, 12) & 0x0060;
                tmpChannel.DCstate2_rsv8 = (chStateArray[13] & 0x01) == 0x01 ? true : false;
                tmpChannel.DCstate2_rsv9 = (chStateArray[13] & 0x02) == 0x02 ? true : false;
                tmpChannel.DCstate2_rsv10 = (chStateArray[13] & 0x04) == 0x04 ? true : false;
                tmpChannel.DCstate2_rsv11 = (chStateArray[13] & 0x08) == 0x08 ? true : false;
                tmpChannel.DCstate2_rsv12 = (chStateArray[13] & 0x10) == 0x10 ? true : false;
                tmpChannel.DCstate2_rsv13 = (chStateArray[13] & 0x20) == 0x20 ? true : false;
                tmpChannel.DCstate2_rsv14 = (chStateArray[13] & 0x40) == 0x40 ? true : false;
                tmpChannel.DCstate2_rsv15 = (chStateArray[13] & 0x80) == 0x80 ? true : false;

                //DC停机原因
                tmpChannel.DCoff_voltageUpper = (chStateArray[14] & 0x01) == 0x01 ? true : false;
                tmpChannel.DCoff_DCoffvoltageLower = (chStateArray[14] & 0x02) == 0x02 ? true : false;
                tmpChannel.DCoff_currentUpper = (chStateArray[14] & 0x04) == 0x04 ? true : false;
                tmpChannel.DCoff_currentLower = (chStateArray[14] & 0x08) == 0x08 ? true : false;
                tmpChannel.DCoff_timeout = (chStateArray[14] & 0x10) == 0x10 ? true : false;
                tmpChannel.DCoff_temperUpper = (chStateArray[14] & 0x20) == 0x20 ? true : false;
                tmpChannel.DCoff_temperLower = (chStateArray[14] & 0x40) == 0x40 ? true : false;
                tmpChannel.DCoff_systemFault = (chStateArray[14] & 0x80) == 0x80 ? true : false;
                tmpChannel.DCoff_offCommand = (chStateArray[15] & 0x01) == 0x01 ? true : false;
                tmpChannel.DCoff_LastPackEnd = (chStateArray[15] & 0x02) == 0x02 ? true : false;
                tmpChannel.DCoff_ARM_auxTemper = (chStateArray[15] & 0x04) == 0x04 ? true : false;
                tmpChannel.DCoff_ARM_auxVolt = (chStateArray[15] & 0x08) == 0x08 ? true : false;
                tmpChannel.DCoff_ARM_emergency = (chStateArray[15] & 0x10) == 0x10 ? true : false;
                tmpChannel.DCoff_rsv13 = (chStateArray[15] & 0x20) == 0x20 ? true : false;
                tmpChannel.DCoff_rsv14 = (chStateArray[15] & 0x40) == 0x40 ? true : false;
                tmpChannel.DCoff_rsv15 = (chStateArray[15] & 0x80) == 0x80 ? true : false;

                tmpChannel.DC_outputVolt = BitConverter.ToSingle(chStateArray, 16);
                tmpChannel.DC_loadCurrent = BitConverter.ToSingle(chStateArray, 20);
                if (BitConverter.ToUInt16(chStateArray, 24) >= 500 && BitConverter.ToUInt16(chStateArray, 24) < 6000)
                {
                    tmpChannel.DC_loadTemper = Convert.ToSingle(1 / (Math.Log(2898.0f / (BitConverter.ToUInt16(chStateArray, 24)) - 0.4f) / 3470.0f + 1 / 298.15f) - 273.15f);
                }
                else tmpChannel.DC_loadTemper = 0;

                if (BitConverter.ToUInt16(chStateArray, 26) >= 500 && BitConverter.ToUInt16(chStateArray, 26) < 6000)
                {
                    tmpChannel.DC_ambientTemper = Convert.ToSingle(1 / (Math.Log(2898.0f / (BitConverter.ToUInt16(chStateArray, 26)) - 0.4f) / 3470.0f + 1 / 298.15f) - 273.15f);
                }
                else tmpChannel.DC_ambientTemper = 0;

                tmpChannel.DC_busVolt = Convert.ToSingle(BitConverter.ToUInt16(chStateArray, 28)) / 1000.0f;
                tmpChannel.chargeCap = BitConverter.ToSingle(chStateArray, 30);
                tmpChannel.dischargeCap = BitConverter.ToSingle(chStateArray, 34);
                tmpChannel.totalCap = BitConverter.ToSingle(chStateArray, 38);
                tmpChannel.pulseNumber = BitConverter.ToUInt16(chStateArray, 42);//脉冲段号
                tmpChannel.stepTime = BitConverter.ToSingle(chStateArray, 42);
                tmpChannel.chargeEnergy = BitConverter.ToSingle(chStateArray, 46);
                tmpChannel.dischargeEnergy = BitConverter.ToSingle(chStateArray, 50);
                tmpChannel.totalEnergy = BitConverter.ToSingle(chStateArray, 54);
                tmpChannel.power = BitConverter.ToSingle(chStateArray, 58);
                if (float.IsNaN(tmpChannel.power))
                {
                    Console.WriteLine(chStateArray[58].ToString()+chStateArray[59].ToString()+chStateArray[60].ToString()+chStateArray[61].ToString());
                }
                tmpChannel.diffVolt = BitConverter.ToSingle(chStateArray, 62);
                tmpChannel.diffCurrent = BitConverter.ToSingle(chStateArray, 66);
                tmpChannel.bydLoadTemper1 = BitConverter.ToUInt16(chStateArray, 70);
                tmpChannel.bydLoadTemper2 = BitConverter.ToUInt16(chStateArray, 72);
                tmpChannel.bydLoadTemper3 = BitConverter.ToUInt16(chStateArray, 74);
                tmpChannel.faultStatus = fualtStatus2string(chStateArray);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace + e.Message);
            }
            return tmpChannel;
        }
        /// <summary>
        /// 通道故障信息转字符串
        /// </summary>
        /// <param name="byteData"></param>
        /// <returns></returns>
        public static string fualtStatus2string(byte[] byteData)
        {
            bool[] arr1 = { (byteData[4] & 0x01) == 0x01 ? true : false,
                            (byteData[4] & 0x02) == 0x02 ? true : false,
                            (byteData[4] & 0x04) == 0x04 ? true : false,
                            (byteData[4] & 0x08) == 0x08 ? true : false,
                            (byteData[5] & 0x02) == 0x02 ? true : false, 
                             (byteData[5] & 0x04) == 0x04 ? true : false, 
                             (byteData[5] & 0x08) == 0x08 ? true : false,
                             (byteData[5] & 0x10) == 0x10 ? true : false, 
                             (byteData[5] & 0x20) == 0x20 ? true : false, 
                             (byteData[5] & 0x40) == 0x40 ? true : false,
                             (byteData[5] & 0x80) == 0x80 ? true : false, 
                             (byteData[6] & 0x01) == 0x01 ? true : false,
                             (byteData[6] & 0x02) == 0x02 ? true : false,
                             (byteData[6] & 0x04) == 0x04 ? true : false,
                             (byteData[6] & 0x08) == 0x08 ? true : false,
                             (byteData[6] & 0x10) == 0x10 ? true : false,
                             (byteData[6] & 0x20) == 0x20 ? true : false,
                             (byteData[6] & 0x40) == 0x40 ? true : false,
                             (byteData[6] & 0x80) == 0x80 ? true : false, 
                             (byteData[7] & 0x01) == 0x01 ? true : false,
                             (byteData[7] & 0x04) == 0x04 ? true : false,
                             (byteData[7] & 0x08) == 0x08 ? true : false,
                             (byteData[7] & 0x10) == 0x10 ? true : false,
                             (byteData[7] & 0x20) == 0x20 ? true : false,
                             (byteData[7] & 0x40) == 0x40 ? true : false,
                             (byteData[7] & 0x80) == 0x80 ? true : false,
                             (byteData[10] & 0x01) == 0x01 ? true : false,
                             (byteData[10] & 0x04) == 0x04 ? true : false,
                             (byteData[10] & 0x20) == 0x20 ? true : false,
                             (byteData[11] & 0x01) == 0x01 ? true : false,
                             (byteData[11] & 0x04) == 0x04 ? true : false,
                             (byteData[12] & 0x10) == 0x10 ? true : false,
                             (byteData[12] & 0x40) == 0x40 ? true : false,
                             (byteData[12] & 0x80) == 0x80 ? true : false, 
                             (byteData[13] & 0x01) == 0x01 ? true : false,
                             (byteData[13] & 0x02) == 0x02 ? true : false,
                             (byteData[13] & 0x04) == 0x04 ? true : false,
                             (byteData[13] & 0x08) == 0x08 ? true : false,
                             (byteData[13] & 0x10) == 0x10 ? true : false,
                             (byteData[13] & 0x20) == 0x20 ? true : false,
                             (byteData[13] & 0x40) == 0x40 ? true : false,
                             (byteData[13] & 0x80) == 0x80 ? true : false,
                             (byteData[14] & 0x01) == 0x01 ? true : false, 
                             (byteData[14] & 0x02) == 0x02 ? true : false,
                             (byteData[14] & 0x04) == 0x04 ? true : false, 
                             (byteData[14] & 0x08) == 0x08 ? true : false, 
                             (byteData[14] & 0x10) == 0x10 ? true : false, 
                             (byteData[14] & 0x20) == 0x20 ? true : false,
                             (byteData[14] & 0x40) == 0x40 ? true : false,
                             (byteData[14] & 0x80) == 0x80 ? true : false,
                             (byteData[15] & 0x01) == 0x01 ? true : false,
                             (byteData[15] & 0x02) == 0x02 ? true : false,
                             (byteData[15] & 0x04) == 0x04 ? true : false, 
                             (byteData[15] & 0x08) == 0x08 ? true : false,
                             (byteData[15] & 0x20) == 0x20 ? true : false, 
                             (byteData[15] & 0x40) == 0x40 ? true : false,
                             (byteData[15] & 0x80) == 0x80 ? true : false, 
                         };
            string s1 = string.Empty;
            int str1;
            foreach (bool bol in arr1)
            {
                if (bol == true)
                    str1 = 1;
                else str1 = 0;
                s1 = s1 + str1;
            }
            string hexStr = HexConvert.BinaryToHex(s1);
            return hexStr;
        }
        /// <summary>
        /// 解析工况请求包
        /// </summary>
        /// <param name="stateData">接收到的字节数组</param>
        /// <param name="chStateLength">字节数组的有效长度</param>
        /// <returns></returns>
        public static CondiRequest byte2CondiRequest(byte[] stateData, int chStateLength)
        {   
            CondiRequest condiReq = new CondiRequest();
            try
            {
                byte[] chStateArray = new byte[chStateLength];
                Array.Copy(stateData, 2, chStateArray, 0, chStateLength);//拷贝请求包数据
                condiReq.GID = BitConverter.ToUInt16(chStateArray, 4);
                condiReq.MID = BitConverter.ToUInt16(chStateArray, 6);
                condiReq.CID = chStateArray[9]; //BitConverter.(chStateArray, 7);
                string req = BitConverter.ToString(chStateArray, 12);
                if (req == "00-FF")
                {
                    condiReq.hasRequest = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace + e.Message);
            }
            return condiReq;
        }
        /// <summary>
        /// 解析设备查询应答包
        /// </summary>
        /// <param name="stateData"></param>
        /// <param name="chStateLength"></param>
        /// <returns></returns>
        public static DeviceInfo byte2DeviceInfo(byte[] stateData,int chStateLength)
        {
            DeviceInfo device = new DeviceInfo();
            try
            {
                byte[] chStateArray = new byte[chStateLength];
                Array.Copy(stateData, 2, chStateArray, 0, chStateLength);
                //device.DeviceId = BitConverter.ToString(chStateArray, 6, 16);
                device.DeviceId = System.Text.Encoding.ASCII.GetString(chStateArray, 6, 16);
                device.DeviceMac = BitConverter.ToString(chStateArray, 22, 1) + BitConverter.ToString(chStateArray, 23, 1)+
                    BitConverter.ToString(chStateArray, 24, 1) + BitConverter.ToString(chStateArray, 25, 1) + BitConverter.ToString(chStateArray, 26, 1)+
                    BitConverter.ToString(chStateArray, 27, 1);
                //device.DeviceMac = Convert.ToString(chStateArray, 22, 6);
                device.DeviceClass = BitConverter.ToUInt16(chStateArray, 28) & 0x00FF;
                device.DeviceType = BitConverter.ToUInt16(chStateArray, 28) >> 8;
                device.DevicePort = BitConverter.ToUInt16(chStateArray, 30);
            }
            catch(Exception e)
            {
                Console.WriteLine(e.StackTrace + e.Message);
            }
            return device;
        }
        /// <summary>
        /// 解析辅助通道数据
        /// </summary>
        /// <param name="stateData"></param>
        /// <param name="aux"></param>
        /// <returns></returns>
        public static AuxChannel GetAuxChannelData(byte[] stateData,AuxChannel aux)
        {
            int auxId = aux.auxId;
            aux.voltage = BitConverter.ToSingle(stateData, 638 + 10 * (aux.auxId - 1));
            aux.temperature1 = Convert.ToSingle(1 / (Math.Log(2898.0f / (BitConverter.ToUInt16(stateData, 642 + 10 * (aux.auxId - 1))) - 0.4f) / 3470.0f + 1 / 298.15f) - 273.15f);
            aux.temperature2 = Convert.ToSingle(1 / (Math.Log(2898.0f / (BitConverter.ToUInt16(stateData, 644 + 10 * (aux.auxId - 1))) - 0.4f) / 3470.0f + 1 / 298.15f) - 273.15f);
            aux.temperature3 = Convert.ToSingle(1 / (Math.Log(2898.0f / (BitConverter.ToUInt16(stateData, 646 + 10 * (aux.auxId - 1))) - 0.4f) / 3470.0f + 1 / 298.15f) - 273.15f); 
            return aux;
        }
    }
}
